package cn.edu.imut.service;

import cn.edu.imut.bean.OrderBean;
import cn.edu.imut.bean.ProductBean;
import cn.edu.imut.bean.UserBean;
import cn.edu.imut.dao.CartDao;
import cn.edu.imut.dao.OrderDao;
import cn.edu.imut.dao.ProductDao;
import cn.edu.imut.dao.UserDao;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Scanner;

public class PayMentService {
    //商品直接支付功能
    public void PayMentByProduct(int userID){
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要购买商品ID:");
        String pid = sc.next();
        System.out.print("请输入购买商品数量:");
        String pqy = sc.next();
        try {
            int product_id = Integer.parseInt(pid);
            int product_quantity = Integer.parseInt(pqy);
            //从数据库中获取商品
            ProductDao productDao = new ProductDao();
            ProductBean productBean = productDao.getProductDetails(product_id);
            if(productBean.getStock()>=product_quantity){
                /*| 字段名          | 数据类型        | 说明                     |
|-----------------|-----------------|--------------------------|
| order_id        | INT (主键)     | 唯一标识                 |
| product_id      | INT            | 外键，关联商品表（product_id） |
| user_id         | INT            | 外键，关联用户表（user_id） |
| order_status    | VARCHAR(20)    | 订单状态（如：待支付、已支付等） |
| order_time      | DATETIME       | 订单创建时间             |
| total_amount    | DECIMAL(10, 2) | 订单总金额               |
| product_quantity| INT            | 商品总数量               |
| address         | TEXT           | 收货地址（冗余字段）     |
| payment_time    | DATETIME       | 支付完成时间             |

                 */
                productBean.setStock(productBean.getStock()-product_quantity);
                productDao.updateProduct(productBean);
                Double Order_total = productBean.getPrice()*product_quantity;
                OrderBean orderBean = new OrderBean(product_id,userID,"待支付", LocalDateTime.now(),Order_total
                        ,product_quantity,null,null);
                OrderDao orderDao=new OrderDao();
                int orderID=orderDao.addOrder(orderBean);
                System.out.print("是否进行支付(【1】是 【2】否 ):");
                String Select = sc.next();
                if (Select.equals("1")) {
                    UserBean userBean = new UserDao().getUserByUserID(userID);
                    if (userBean.getAccountBalance() >= Order_total) {
                        userBean.setAccountBalance(userBean.getAccountBalance()-Order_total);
                        orderBean = orderDao.getOrderById(orderID);
                        orderBean.setOrderStatue("已支付");
                        orderBean.setPaymentTime(LocalDateTime.now());
                        orderDao.updateOrder(orderBean);
                        System.out.println("订单已支付成功。");
                    } else {
                    System.out.println("账户余额不足。");
                    }
                }

            } else {
                System.out.println("商品库存不够，无法继续支付。");
            }
        }catch (NumberFormatException e) {
            System.out.println("请正确输入(商品id和数量只能输入数字)!");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
